package com.Adapter;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bolao.R;
import com.entidades.Team;
 
public class CustomTeamAdapter extends BaseAdapter implements NothingSelectedItem {
	private List<Team> teams;
	private LayoutInflater mInflater;
	private Context mContext;
	private static final int EXTRA = 1;
	
	static class ViewHolder{
		private ImageView img;
		private TextView teamName;			
	}
	
	public CustomTeamAdapter(Context context, List<Team> teams) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		this.teams = teams;

	}
		
	@Override
	public int getCount() {
		return teams.size()+EXTRA;
	}

	@Override
	public Team getItem(int arg0) {
		if(arg0!=0)
		return teams.get(arg0 -EXTRA);
		else return null;
	}

	@Override
	public long getItemId(int arg0) {
		if(arg0!=0)
		return teams.get(arg0 - EXTRA).getId();
		else return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		
		ViewHolder holder;
		if (convertView == null || convertView.getTag() == null) {
			convertView = mInflater.inflate(R.layout.team_row_layout, null);
			holder = new ViewHolder();  
			holder.img = (ImageView) convertView.findViewById(R.id.teamFlag);
            holder.teamName = (TextView) convertView.findViewById(R.id.teamName);
            convertView.setTag(holder);
                  
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(position!=0){
		Resources r = mContext.getResources();		
		Team team = teams.get(position - EXTRA);
		holder.img.setImageResource(r.getIdentifier(team.getFlag(), "drawable",  mContext.getPackageName()));
		holder.teamName.setText(team.getLocalizedName());

		
		return convertView;
 
		} else{
			
			return nothingSelected(parent);
		}

	}

	@Override
	public View nothingSelected(ViewGroup parent) {
		
		return mInflater.inflate(R.layout.spinner_row_nothing_selected, parent, false);
    
	}




}
